package com.qyy.remotemonitor.common.di;

import android.content.Context;
import android.text.TextUtils;

import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.qyy.remotemonitor.http.ApiService;
import com.qyy.remotemonitor.http.CookieInterceptor;
import com.qyy.remotemonitor.http.HttpEntity;
import com.wang.android.library.di.http.Http;
import com.wang.android.library.di.modules.AppModule;
import com.wang.android.library.tool.WSharedPreference;

import dagger.Module;
import dagger.Provides;

/**
 * Application Module
 *
 * @author wujiajun
 */
@Module
public class AppCommonModule extends AppModule {

    private Http mHttp;
    public AppCommonModule(Context context) {
        super(context);
        initHttp();
//        initUtils();
//        initRealm(context);
    }

    /**
     * Http初始化
     */
    private void initHttp() {
        //cookie cache & persistor
        ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(provideContext()));
        // host url
        String oldUrl = WSharedPreference.getInstance().getString("HOST_URL","");
        if(!TextUtils.isEmpty(oldUrl))
            Http.setHttpHost(oldUrl);
        mHttp = new Http.HttpBuilder()
                .setBaseUrl(Http.getHttpHost())
                .setCookieJar(cookieJar)
                .setTimeout(60)
                .addInterceptor(new CookieInterceptor())
                .build();
    }

    /**
     * Utils库初始化
     */
    private void initUtils() {
//        Utils.init(provideContext());
    }

    /**
     * init Realm
     *
     * @param context
     */
    private void initRealm(Context context) {
//        Realm.init(context);
    }

    @Provides ApiService provideApiService() {
        return mHttp.getRetrofit().create(ApiService.class);
    }
//
//    @Provides
//    Realm provideRealm() {
//        return Realm.getDefaultInstance();
//    }

    /**
     * 注入类
     * @return
     */
    @Provides HttpEntity getHttpEntity(){
        return new HttpEntity();
    }

}
